//
//  ZMPresentationManager.swift
//  ZMWB
//
//  Created by 朱敏 on 2017/10/10.
//  Copyright © 2017年 朱敏. All rights reserved.
//

import UIKit

// 自定义转场展现
let ZMPresentationManagerDidPresented = "ZMPresentationManagerDidPresented"
// 自定义转场消失
let ZMPresentationManagerDidDismissed = "ZMPresentationManagerDidDismissed"

class ZMPresentationManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    // 定义标记记录当前是否展现
    var isPresent = false
    // 保存菜单的尺寸
    var presentFrame = CGRect.zero
    
    // MARK: -UIViewControllerTransitioningDelegate
    // 该方法用于返回一个负责转场动画的对象
    // 可以在该对象中控制弹出视图的尺寸等
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        let pc = ZMPresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    // 该方法用于返回一个负责转场如何出现的对象
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        // 发送一个通知，告诉调用者状态发生了改变
        NotificationCenter.default.post(name: NSNotification.Name(ZMPresentationManagerDidPresented), object: self)
        return self
    }
    
    // 该方法用于返回一个负责转场如何消失的对象
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        // 发送一个通知，告诉调用者状态发生了改变
        NotificationCenter.default.post(name: NSNotification.Name(ZMPresentationManagerDidDismissed), object: self)
        return self
    }
    
    // MARK: -UIViewControllerAnimatedTransitioning
    // 告诉系统展现和消失的动画时长
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    // 专门用于管理modal如何展现和消失的，无论是展现还是消失都会调用该方法
    // 注意点：只要我们实现了这个代理方法，那么就不会再有默认的动画了
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // 0.判断当前是展现还是消失
        if isPresent {
            // 展现
            willPresentedController(transitionContext: transitionContext)
        } else {
            willDismissedController(transitionContext: transitionContext)
        }
    }

    // 执行展现动画
    private func willPresentedController(transitionContext: UIViewControllerContextTransitioning) {
        // 1.获取需要弹出的视图
        // 通过ToViewKey取出的就是toVC对应的view
        guard let toView = transitionContext.view(forKey: .to) else {
            return
        }
        // 2.将要弹出的视图添加到containerView上
        transitionContext.containerView.addSubview(toView)
        // 3.执行动画
        toView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        // 设置锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            toView.transform = CGAffineTransform.identity
        }) { (_) in
            // 注意： 自定义转场动画，在执行完动画之后一定要告诉系统，动画执行完毕了
            transitionContext.completeTransition(true)
        }
    }
    
    // 执行消失动画
    private func willDismissedController(transitionContext: UIViewControllerContextTransitioning) {
        // 消失
        // 1.拿到需要消失的视图
        guard let fromView = transitionContext.view(forKey: .from) else {
            return
        }
        // 2.执行动画让视图消失
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            fromView.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
        }, completion: { (_) in
            // 注意：自定转场动画，在执行完动画之后一定要告诉系统动画执行完毕了
            transitionContext.completeTransition(true)
        })

    }
}
